package com.book.breakage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.breakage.entity.Accident;
import com.book.breakage.mapper.AccidentMapper;
import com.book.breakage.service.AccidentService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 张高赫
 * @Description:  20173954
 */

@Service
public class AccidentServiceImpl extends ServiceImpl<AccidentMapper, Accident> implements AccidentService {
    @Autowired
    AccidentMapper accidentMapper;

    @Override
    public List<Accident> accidentList(Accident accident) {
        QueryWrapper<Accident> accidentQueryWrapper = new QueryWrapper<>();
        return this.accidentMapper.selectList(accidentQueryWrapper);
    }

    @Override
    public IPage<Accident> accidentPage(QueryRequest request, Accident accident) {
        QueryWrapper<Accident> accidentQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(accident.getBookname())) {
            accidentQueryWrapper.like("bookname", accident.getBookname());
        }
        if (StringUtils.isNotEmpty(accident.getPrincipal())) {
            accidentQueryWrapper.like("principal", accident.getPrincipal());
        }
        Page<Accident> accidentPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(accidentPage, accidentQueryWrapper);
    }

    @Override
    public void accidentAdd(Accident accident) {
        this.save(accident);
    }

    @Override
    public void accidentUpdate(Accident accident) {
        this.saveOrUpdate(accident);
    }

    @Override
    public void accidentDelete(String[] breReturn) {
        List<String> list = Arrays.asList(breReturn);
        this.removeByIds(list);
    }

    @Override
    public List<Accident> accidentApply(String[] accident) {
        List<String> list = Arrays.asList(accident);
        return (List<Accident>)this.listByIds(list);
    }
}
